package cn.tqfeiyang.smartadmin.commons.pagination;

import cn.tqfeiyang.smartadmin.commons.Constants;
import cn.tqfeiyang.smartadmin.commons.toolbox.ObjectUtils;
import cn.tqfeiyang.smartadmin.commons.toolbox.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;

@Data
public class QueryParams {

    @Schema(description = "页码，默认为 1", example = "1")
    private Integer pageNum = Constants.PAGE_DEFAULT_INDEX;

    @Schema(description = "页大小，默认为 10", example = "10")
    private Integer pageSize = Constants.PAGE_DEFAULT_SIZE;

    @Schema(description = "排序字段")
    private String orderBy = "";

    @Schema(description = "排序方式 asc：升序，desc：降序")
    private OrderDirection direction = null;

    public Integer getPageNum() {
        if (pageNum == null || pageNum <= 0) {
            pageNum = Constants.PAGE_DEFAULT_INDEX;
        }
        return pageNum;
    }

    public Integer getPageSize() {
        if (pageSize == null || pageSize <= 0) {
            pageSize = Constants.PAGE_DEFAULT_SIZE;
        }
        return pageSize;
    }

    /**
     * 获取 MybatisPlus 的 Page 对象，包含页码和页大小。
     *
     * @return age 对象
     */
    @JsonIgnore
    public <T> Page<T> getPageObject() {
        QueryHelper helper = new QueryHelper(this);
        return helper.getPageObject();
    }

    /**
     * 获取 MybatisPlus 的 Wrapper 对象，包含查询条件。
     *
     * @return Wrapper 对象
     */
    @JsonIgnore
    public <T> Wrapper<T> getWrapper() {
        QueryHelper helper = new QueryHelper(this);
        return helper.getWrapper();
    }

    /**
     * 排序字段名
     */
    private final String ORDER_BY_FIELD = "orderBy";

    /**
     * 排序顺序字段名
     */
    private final String ORDER_DIRECTION_FIELD = "direction";

    /**
     * 获取排序条件，注意不包含 order by
     *
     * @return 返回排序条件，没有排序条件时返回null
     */
    public String getCustomOrderBySegment() {
        Field orderByField = ReflectionUtils.findField(getClass(), ORDER_BY_FIELD);
        if (orderByField == null) {
            return null;
        } else {
            if (!(orderByField.getType().isEnum() || String.class.isAssignableFrom(orderByField.getType()))) {
                throw new RuntimeException("查询请求类" + getClass().getName() + "中的" + ORDER_BY_FIELD + "字段必须是枚举类型或字符串类型");
            }
        }

        Object orderByValue = ReflectionUtils.getField(orderByField, this);
        if (orderByValue == null) {
            return null;
        }

        Field directionField = ReflectionUtils.findField(getClass(), ORDER_DIRECTION_FIELD);
        if (directionField == null) {
            return null;
        } else {
            if (!(OrderDirection.class.isAssignableFrom(directionField.getType()))) {
                throw new RuntimeException("查询请求类" + getClass().getName() + "中的" + ORDER_DIRECTION_FIELD + "字段必须是OrderDirection枚举类型");
            }
        }

        Object directionValue = ReflectionUtils.getField(directionField, this);
        if (directionValue == null || !(directionValue instanceof OrderDirection)) {
            directionValue = OrderDirection.asc;
        }

        return StringUtils.camelToUnderline(orderByValue.toString()) + " " + directionValue.toString();
    }

}